//
//  GQHRouteTrucksController.m
//  Xanthium
//
//  Created by GuanQinghao on 09/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#pragma mark Macro
#import "GQHHeader.h"

#pragma mark Model

#pragma mark Object

#pragma mark View
#import "GQHRouteTrucksView.h"

#pragma mark Controller
#import "GQHRouteTrucksController.h"
#import "GQHTruckInfoController.h"

#pragma mark CocoaPods

#pragma mark ----------Header End----------

@interface GQHRouteTrucksController () <GQHRouteTrucksViewDelegate> {
    
#pragma mark Data
    /// 请求参数
    __block NSMutableDictionary *parameters;
    /// 返回模型数组
    __block NSMutableArray<GQHTruckModel *> *truckModelArray;
#pragma mark View
    /// 自定义根视图
    GQHRouteTrucksView *rootView;
}

@end


@implementation GQHRouteTrucksController

#pragma mark --Lifecycle
- (void)loadView {
    DLog();
    
    rootView = [[GQHRouteTrucksView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    rootView.qh_delegate = self;
    self.view = rootView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    DLog();
    
    parameters = [NSMutableDictionary dictionary];
    [parameters setObject:USER_GET_ID forKey:@"token"];
    [parameters setObject:_qh_routeModel.qh_departureID forKey:@"originId"];
    [parameters setObject:_qh_routeModel.qh_destinationID forKey:@"destinationId"];
    
    [self fetchServiceDataWithParameters:parameters];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    DLog();
    
    self.qh_titleLabel.text = [NSString stringWithFormat:@"%@-%@",_qh_routeModel.qh_departure,_qh_routeModel.qh_destination];
    self.qh_rightButton.hidden = YES;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [rootView.qh_tableView.mj_header beginRefreshing];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    DLog();
    
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
    [rootView.qh_tableView.mj_header endRefreshing];
}

#pragma mark --Delegate
- (void)qh_selectedRowWithTableView:(UITableView *)tableView atIndexPath:(NSIndexPath *)indexPath {
    
    GQHTruckInfoController *truckInfoController = [[GQHTruckInfoController alloc] init];
    truckInfoController.qh_freeTruck = @"YES";
    truckInfoController.qh_truckID = truckModelArray[indexPath.row].qh_truckID;
    truckInfoController.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:truckInfoController animated:YES];
}

#pragma mark --TargetMethod

#pragma mark --PrivateMethod
- (void)fetchServiceDataWithParameters:(NSMutableDictionary *)parameters {
    
    __block int page = 1;
    
    [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
    [parameters setObject:ROWS forKey:@"rows"];
    
    rootView.qh_tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        page = 1;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHTruckModel qh_fetchFreeTrucksWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHTruckModel *> *trucks) {
            
            if (status) {
                
                truckModelArray = [NSMutableArray arrayWithArray:trucks];
                rootView.qh_truckModelArray = truckModelArray;
                [rootView.qh_tableView reloadData];
            } else {
                
                [self showToastWithText:message];
            }
            [rootView.qh_tableView.mj_header endRefreshing];
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_header endRefreshing];
        }];
    }];
    
    rootView.qh_tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        
        page++;
        [parameters setObject:[NSNumber numberWithInt:page] forKey:@"page"];
        
        [GQHTruckModel qh_fetchFreeTrucksWithParameters:parameters success:^(BOOL status, NSNumber *code, NSString *message, NSArray<GQHTruckModel *> *trucks) {
            
            if (status) {
                
                if (trucks && trucks.count > 0) {
                    
                    [truckModelArray addObjectsFromArray:trucks];
                    rootView.qh_truckModelArray = truckModelArray;
                    [rootView.qh_tableView reloadData];
                    [rootView.qh_tableView.mj_footer endRefreshing];
                } else {
                    
                    [rootView.qh_tableView.mj_footer endRefreshingWithNoMoreData];
                }
            } else {
                
                [self showToastWithText:message];
                [rootView.qh_tableView.mj_footer endRefreshing];
            }
        } failure:^(NSError *error) {
            
            NSString *errorString = [NSString stringWithFormat:@"网络出错:%@,code:%ld", error.domain, (long)error.code];
            [self showToastWithText:errorString];
            [rootView.qh_tableView.mj_footer endRefreshing];
        }];
    }];
}
#pragma mark --Setter

#pragma mark --Getter

#pragma mark --View

@end
